package com.fire.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.fire.service.ProviderService;
import com.fire.util.MsgInfo;
import com.fire.util.base.ResultBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/provider")
@Api(description = "供应商模块API")
public class ProviderController extends BaseController {

	@Autowired
	private ProviderService providerService;

	/**
	 * 供应商申请认证接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "申请认证", notes = "供应商通过此API申请认证")
	@ApiImplicitParam(name = "json入参", value = "companyName,location,companyLicenseZipUrl,legalPersonName,idCardNum,idCardPhotoIds", required = true, dataType = "String")
	@RequestMapping(value = "/identify.do", method = RequestMethod.POST)
	public ResultBody identify(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.identify(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 产品发布接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "产品发布", notes = "供应商通过此API产品发布")
	@ApiImplicitParam(name = "json入参", value = "productName,packageFormat,validDate,pubPrice,pubAmount,pubType,advertiseContent,productInfoIds,qualificationIds", required = true, dataType = "String")
	@RequestMapping(value = "/pubProduct.do", method = RequestMethod.POST)
	public ResultBody pubProduct(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.pubProduct(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 文件上传接口
	 * 
	 * @param files
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "图片上传", notes = "供应商通过此API进行图片上传")
	@ApiImplicitParam(name = "文件入参", value = "files", required = true, dataType = "MultipartFile")
	@RequestMapping(value = "/fileUpload.do", method = RequestMethod.POST)
	public ResultBody fileUpload(@RequestParam("files") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) {
		ResultBody result = null;
		try {
			result = providerService.fileUpload(files);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 产品列表的信息接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "产品列表", notes = "供应商通过此API查看产品列表")
	@RequestMapping(value = "/productList.do", method = RequestMethod.POST)
	public ResultBody productList(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.productList(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 压缩包上传接口
	 * 
	 * @param files
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "压缩包上传", notes = "供应商通过此API进行压缩包上传")
	@ApiImplicitParam(name = "文件入参", value = "zip", required = true, dataType = "MultipartFile")
	@RequestMapping(value = "/zipUpload.do", method = RequestMethod.POST)
	public ResultBody zipUpload(@RequestParam("zip") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) {
		ResultBody result = null;
		try {
			result = providerService.zipUpload(file);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 消息列表接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "消息列表", notes = "供应商通过此API查看系统消息列表")
	@RequestMapping(value = "/notification.do", method = RequestMethod.POST)
	public ResultBody notification(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.notification(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 消息设置为已读接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "消息设为已读", notes = "供应商通过此API设置系统消息为已读")
	@RequestMapping(value = "/setIsRead.do", method = RequestMethod.POST)
	public ResultBody setIsRead(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.setIsRead(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 删除消息接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "删除消息", notes = "供应商通过此API设置删除消息")
	@RequestMapping(value = "/deleteNotification.do", method = RequestMethod.POST)
	public ResultBody deleteNotification(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.deleteNotification(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 取消申请接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "取消申请", notes = "供应商通过此API取消产品发布申请")
	@RequestMapping(value = "/cancelPublish.do", method = RequestMethod.POST)
	public ResultBody cancelPublish(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.cancelPublish(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 修改用户基本信息接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "修改用户基本信息", notes = "供应商通过此API修改用户基本信息")
	@RequestMapping(value = "/updateUserInfo.do", method = RequestMethod.POST)
	public ResultBody updateUserInfo(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		Integer userId = super.currentUserId(request);
		try {
			result = providerService.updateUserInfo(params, userId);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

}
